/* EasyCODE V8 */
/* EasyCODE ( 0 */
#include "JI2C.h"
#include "MPU.h"
/* EasyCODE ( 0 
unsigned char MPURead(unsigned char address) */
/* EasyCODE F */
unsigned char MPURead(unsigned char address)
{
    unsigned char data;
    
    i2c_start();
    i2c_write(MPU_W);	// write 0xD2
    i2c_write(address);	// write register address
    
    i2c_repStart();
    i2c_write(MPU_R);	// write 0xD3
    data = i2c_read();	// Get MSB result
    i2c_nack();
    i2c_stop();
    return data;
}

/* EasyCODE ) */
/* EasyCODE ( 0 
void MPUWrite(unsigned char address, unsigned char data) */
/* EasyCODE F */
void MPUWrite(unsigned char address, unsigned char data)
{
    i2c_start();
    i2c_write(MPU_W);	// write 0xD2
    i2c_write(address);	// write register address
    i2c_write(data);
    i2c_stop();
}

/* EasyCODE ) */
/* EasyCODE ( 0 
void MPU(unsigned char address) */
/* EasyCODE F */
void MPU(float *GX, float *GY, float *GZ, float *AX, float *AY, float *AZ, float *Temp)
{
    signed short data;
    i2c_start();
    i2c_write(MPU_W);	// write 0xD0
    i2c_write(MPU_RA_ACCEL_XOUT_H);	// write register address
    
    i2c_repStart();
    i2c_write(MPU_R);	// write 0xD1
    
    data=((i2c_read())<<8);
    i2c_ack();
    *AX=data+(i2c_read());
    i2c_ack();
    data=((i2c_read())<<8);
    i2c_ack();
    *AY=data+(i2c_read());
    i2c_ack();
    data=((i2c_read())<<8);
    i2c_ack();
    *AZ=data+(i2c_read());
    i2c_ack();
    
    data=((i2c_read())<<8);
    i2c_ack();
    *Temp=data+(i2c_read());
    i2c_ack();
    
    data=((i2c_read())<<8);
    i2c_ack();
    *GX=data+(i2c_read());
    i2c_ack();
    data=((i2c_read())<<8);
    i2c_ack();
    *GY=data+(i2c_read());
    i2c_ack();
    data=((i2c_read())<<8);
    i2c_ack();
    *GZ=data+(i2c_read());
    i2c_nack();
    i2c_stop();
    
    //2G
    /**AX=*AX/16384;
    *AY=*AY/16384;
    *AZ=*AZ/16384;*/
    //4G
    *AX=*AX/8192;
    *AY=*AY/8192;
    *AZ=*AZ/8192;
    
    *GX=*GX/65.5;
    *GY=*GY/65.5;
    *GZ=*GZ/65.5;
    
    *Temp=*Temp/340+36.53;
}

/* EasyCODE ) */
/* EasyCODE ) */
